package com.youlai.boot.modules.resource.controller;

import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.resource.model.ResourceForm;
import com.youlai.boot.modules.resource.model.ResourceVO;
import com.youlai.boot.modules.resource.service.ResourceService;
import com.youlai.boot.modules.resource.model.ResourcePageQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Tag(name = "资源接口")
@RestController
@RequestMapping("/api/v1/resource")
@RequiredArgsConstructor
public class ResourceController {

    private final ResourceService resourceService;

    @Operation(summary = "资源分页列表")
    @GetMapping("/page")
    public PageResult<ResourceVO> listResources(ResourcePageQuery query) {
        Page<ResourceVO> result = resourceService.getResourcePage(query);
        return PageResult.success(result);
    }

    @Operation(summary = "资源详情")
    @GetMapping("/{id}")
    public Result<ResourceVO> getResource(@PathVariable Long id) {
        ResourceVO resource = resourceService.getResourceById(id);
        return Result.success(resource);
    }

    @Operation(summary = "添加资源")
    @PostMapping
    public Result<Long> addResource(@Valid @RequestBody ResourceForm resource) {
        Long id = resourceService.saveResource(resource);
        return Result.success(id);
    }

    @Operation(summary = "修改资源")
    @PutMapping("/{id}")
    public Result<Boolean> updateResource(
            @PathVariable Long id,
            @Valid @RequestBody ResourceForm resource) {
        resource.setId(id);
        resourceService.updateResource(resource);
        return Result.success(true);
    }

    @Operation(summary = "删除资源")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(@PathVariable Long id) {
        boolean result = resourceService.deleteResource(id);
        return Result.judge(result);
    }
}